use std::env;
use agent::generic::ProviderConfig;
use autosite::to_markdown::{make_markdown_callbox, MarkdownConfig};
use autosite::{TextSplitterConfig, TextType};
use automation::spider_utils::CrawlResult;
use common::compose::make_empty_ctx;
use dotenvy::dotenv;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化环境变量和日志
    dotenv().ok();
    env_logger::init();

    // 获取 API 密钥
    let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY 环境变量未设置");

    
    let item = CrawlResult {
        content: std::fs::read_to_string("./output/chunked_result.html").expect("Failed to read file"),
        url: "https://cisco.github.io/ChezScheme/csug/foreign.html".to_string()
    };
    let crawl_results = vec![item];

    // 创建 Markdown 转换配置
    let markdown_config = MarkdownConfig {
        output_dir: "output/md".to_string(),
        html_to_md_provider_config: ProviderConfig::OpenAI {
            api_key,
            base_url: env::var("OPENAI_BASE_URL").ok(),
            temperature: 0.3,
            max_tokens: None,
        },
        html_to_md_model: "gpt-4o-mini".to_string(),
        text_splitter_config: TextSplitterConfig {
            text_type: TextType::Html,
            max_tokens: 10000,
        },
        parallel_num: 4,
    };

    // 创建 Markdown 转换器
    println!("开始转换 HTML 到 Markdown...");
    let markdown_callbox = make_markdown_callbox(markdown_config);

    // 执行转换
    let markdown_results = markdown_callbox.run(crawl_results, make_empty_ctx()).await?;
    println!("转换完成，共转换 {} 个页面", markdown_results.len());

    // 输出结果
    for result in &markdown_results {
        println!("URL: {}", result.url);
        println!("语言: {}", result.lang);
        if let Some(path) = &result.output_path {
            println!("输出文件: {}", path.display());
        }
        println!("内容长度: {} 字节", result.content.len());
        println!("---");
    }

    Ok(())
}
